package com.eroad.offer.sina;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.next.intf.ITaskListener;
import com.next.net.SHGet;
import com.next.net.SHPostTaskM;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.sweetdialog.SweetDialog;
import com.sky.zzh.BaseActivity;
import com.sky.zzh.SHApplication;
import com.sky.zzh.SHContainerActivity;
import com.sky.zzh.home.GetCodeActivity;
import com.sky.zzh.home.ZZHMainActivity;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;

/**
 * 第三方登录
 * 
 * @author demon
 * 
 */
public class OtherLogin implements ITaskListener {

	private Context context;

	private LoginInfo mInfo;

	private SHPostTaskM loginTask,userInfoTask;
	private SHGet infoTask;

	public OtherLogin(LoginInfo info, Context context) {
		this.mInfo = info;
		this.context = context;
	}

	public void login() {
//		SHDialog.ShowProgressDiaolg(context, "正在同步数据，请稍候...");
//		loginTask = new SHPostTaskM();
//		loginTask.setUrl(ConfigDefinition.URL + "oauthvalidation");
//		loginTask.getTaskArgs().put("Token", mInfo.getToken());
//		loginTask.getTaskArgs().put("LoginType", mInfo.getType());
//		loginTask.getTaskArgs().put("UId", mInfo.getOpenid());
//		loginTask.setListener(this);
//		loginTask.start();
		ConfigDefinition.photourl = mInfo.getUserphoto();
		if("qq".equals(UserInfoManager.getInstance().getLoginType())){
			UserInfoManager.getInstance().setPhotoQQ(mInfo.getUserphoto());
			UserInfoManager.getInstance().setNickQQ(mInfo.getNickname());
		}else if("sina".equals(UserInfoManager.getInstance().getLoginType())){
			UserInfoManager.getInstance().setPhotoSina(mInfo.getUserphoto());
			UserInfoManager.getInstance().setNickSina(mInfo.getNickname());
		}else if("weixin".equals(UserInfoManager.getInstance().getLoginType())){
			UserInfoManager.getInstance().setPhotoWeixin(mInfo.getUserphoto());
			UserInfoManager.getInstance().setNickWeixin(mInfo.getNickname());
		}
		UserInfoManager.getInstance().setPhotoUrl(mInfo.getUserphoto());
		UserInfoManager.getInstance().setUsername(mInfo.getNickname());
		UserInfoManager.getInstance().sync(context, true);
		requestInfo();
		

	}
	
	private void requestInfo() {
		SHDialog.ShowProgressDiaolg(context);
		infoTask = new SHGet();
		infoTask.setUrl(ConfigDefinition.URL + "user/UserInfo");
		infoTask.setListener(this);
		infoTask.getTaskArgs().put("UserID", mInfo.getType()+"$"+mInfo.getOpenid());
		infoTask.start();
	}
	
	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		SHDialog.dismissProgressDiaolg();
//		if(task == loginTask){
//			JSONObject json = new JSONObject(task.getResult().toString());
//			SHEnvironment.getInstance().setSession(json.getString("SessionId"));
//			//			UserInfoManager.getInstance().setName(mEt_username.getText().toString().trim());
//			//			UserInfoManager.getInstance().setPassword(mEt_password.getText().toString().trim());
//			UserInfoManager.getInstance().setPersonInfoState(json.getBoolean("PersonInfoState"));
//			//			UserInfoManager.getInstance().setAutoLogin(tbAgree.isChecked());
//			UserInfoManager.getInstance().setAutoLogin(true);
//			UserInfoManager.getInstance().sync(context, true);
//			UserInfoManager.getInstance().print();
//			userInfoTask = new SHPostTaskM();
//			userInfoTask.setUrl(ConfigDefinition.URL+"getuserinformationbysession");
//			userInfoTask.setListener(this);
//			userInfoTask.start();
//			if(!json.getBoolean("PersonInfoState")){// 必须完善资料
//				Intent intent1 = new Intent( context,SHContainerActivity.class);
//				intent1.putExtra("class", SelfInfoFragment.class.getName());
//				intent1.putExtra("isFirst",true);
//				((BaseActivity) context).startActivity(intent1);
//			}
//			Intent intent = new Intent();
////			intent.setAction(ConfigDefinition.INTENT_LOGIN_STATUS_CHANGED);
////			intent.putExtra("status", ConfigDefinition.LoginStatus.on);
//			SHApplication.getInstance().sendBroadcast(intent);
//			((BaseActivity) context).finish();
//			((BaseActivity) context).overridePendingTransition(0, R.anim.base_slide_right_out);
//
//
//
//		}else if(task == userInfoTask){
//			JSONObject json = new JSONObject(task.getResult().toString());
//			UserInfoManager.getInstance().setMoblie(json.getString("MobileNumber"));
//			UserInfoManager.getInstance().setEmail(json.getString("EmailAddress"));
//			UserInfoManager.getInstance().setDisplayName(json.getString("DisplayName"));
//			UserInfoManager.getInstance().setUserId(json.getInt("UserInformationID") + "");
////			UserInfoManager.getInstance().setNickName(json.getString("NickName"));
//			UserInfoManager.getInstance().sync(context, true);
//
//		}
		
		
		System.out.println(task.getResult());
		JSONObject json = new JSONObject(task.getResult().toString());
		if ("200".equals(json.optString("ReturnCode"))) {
			UserInfoManager.getInstance().setUserId(mInfo.getType()+"$"+mInfo.getOpenid());
			UserInfoManager.getInstance().sync(context, true);
			Intent intent = new Intent(context,ZZHMainActivity.class);
			context.startActivity(intent);
			((Activity)context).finish();
		}else{
			Intent intent = new Intent(context,GetCodeActivity.class);
//			intent.putExtra("username", mInfo.getNickname());
			intent.putExtra("loginType", mInfo.getType());
			intent.putExtra("openid", mInfo.getOpenid());
			context.startActivity(intent);
			((Activity)context).finish();
		}
	}

	@Override
	public void onTaskFailed(SHTask task) {
//		SHDialog.dismissProgressDiaolg();
		if(task == loginTask && task.getRespInfo().getCode() ==201){
			Intent intent = new Intent(context,SHContainerActivity.class);
//			intent.putExtra("class", OfferPhoneRegist.class.getName());
			intent.putExtra("Token", mInfo.getToken());
			intent.putExtra("LoginType", mInfo.getType());
			intent.putExtra("UId", mInfo.getOpenid());
			context.startActivity(intent);
			((BaseActivity)context).finish();
		}else{
			new SweetDialog(SHApplication.getInstance().getCurrentActivity()).setTitleText("提示").setContentText(task.getRespInfo().getMessage()).show();
		}

	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub

	}
}
